package com.yixing.tech.common.vo.s5;

import com.yixing.tech.common.base.BaseVO;
import com.yixing.tech.common.base.ConditionsVO;
import com.yixing.tech.common.vo.s3.ButtonVO;
import com.yixing.tech.common.vo.s3.DictVO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author yixing tech
 * at 2022/3/21
 * describe：
 */
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class EntitySchema extends BaseVO {
    private String name;
    private String code;
    private String desc;
    private String status;
    private DictVO group;
    /**
     * default,process
     */
    private String type;
    private List<AttrVO> attrs;
    private List<AttrConfig> formConfig;
    private List<AttrConfig> addConfig;
    private List<AttrConfig> titleConfig;
    private List<AttrConfig> conditionConfig;
    private List<AttrConfig> previewConfig;
    private List<AttrConfig> appletTitleConfig;
    private List<AttrConfig> h5TitleConfig;
    List<ButtonVO> buttonConfig;
    /**
     * 用于显示对象名称
     */
    private String agentAttrCode;
    /**
     * 前钩
     */
    private Boolean enablePreHook;
    /**
     * 后钩
     */
    private Boolean enableAfterHook;
    /**
     * 控件前端配置
     */
    Map config;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class AttrVO implements Comparable<AttrVO> {
        String type;
        String name;
        String code;
        /**
         * 是否生成索引
         */
        Boolean isIndex = false;
        Object reference;
        List<BackReference> backReferences;
        List<ConditionsVO.Condition> filter;
        Object idleValue;
        String valueUnit;
        String valueFormat;
        String objId;
        String desc;
        Constraint constraint;
        Sort sortCondition;
        double sort;
        /**
         * 是否支持快速新增
         */
        Boolean fastInsertAble = true;
        /**
         * 是否启用合计
         */
        Boolean statisticsAble = false;
        /**
         * 控件前端配置
         */
        Map config;
        /**
         * 属性布局宽度
         */
        int gridWidth = 24;
        double width = 100;
        /**
         * 关联字段，默认是id
         */
        String relationalField;

        @Override
        public int compareTo(AttrVO o) {
            return Double.compare(sort, o.getSort());
        }

        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public static class InsertEventReference {
            String meteObj;
            String insertList;
            Map<String, String> insertConfig;

            /**
             * 聚合配置，数据已存在不新增，做累加
             */
            AggregationConfig aggregationConfig;

            @Data
            @AllArgsConstructor
            @NoArgsConstructor
            public static class AggregationConfig{
                /**
                 * 分组查询字段
                 */
                List<String> groupKeys;
                /**
                 * 数值累加字段
                 */
                List<String>accrualKeys;
            }
        }

        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public static class UpdateEventReference {
            String updateList;
            List<BackReference> updateConfig;
        }
        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public static class BackReference {
            String reference;
            String formulas;
        }

        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public static class Constraint {
            String reference;
            String formulas;
            String desc;
        }

        @Override
        public String toString() {
            return "AttrVO{" +
                    "type='" + type + '\'' +
                    ", name='" + name + '\'' +
                    ", code='" + code + '\'' +
                    ", sort=" + sort +
                    '}';
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class AttrConfig {
        String code;
        Boolean isNecessary;
        Boolean isUnique;
        Boolean isFocus;
        List<String> symbols;
        Object defaultValue;
        String reference;
        String key;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof AttrConfig)) return false;
            AttrConfig config = (AttrConfig) o;
            return Objects.equals(code, config.code);
        }

        @Override
        public int hashCode() {

            return Objects.hash(code);
        }
    }

    /**
     * 返回属性名称
     *
     * @param code
     * @return
     */
    public String getAttrName(String code) {
        for (AttrVO attrVO : attrs) {
            if (code.equals(attrVO.getCode())) {
                return attrVO.getName();
            }
        }
        return code;
    }


}
